package com.example.smart.ui.activity.home

import com.example.lib.constants.JumpConstants
import com.example.lib.extend.getIntentInt
import com.example.lib.extend.goneLayout
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.extend.verticalManager
import com.example.lib.utils.statusbar.StatusBarUtils
import com.example.smart.R
import com.example.smart.adapter.home.MySheepAdapter
import com.example.smart.base.adapter.BaseRecyclerAdapter
import com.example.smart.base.ui.activity.BaseTitleBarActivity
import com.example.smart.databinding.ActivityMySheepBinding
import com.example.smart.dialog.DeleteDialog
import com.example.smart.room.bean.AnimalBean
import com.example.smart.room.factory.RoomFactory
import com.example.smart.utils.DisplayUtils

class MySheepActivity : BaseTitleBarActivity<ActivityMySheepBinding>() {
    private var id = 0
    private var deletePosition = -1
    private var deleteID = -1L
    private val mResponse by lazy {
        RoomFactory.createAnimalRepository()
    }

    private val mAdapter by lazy {
        MySheepAdapter(this@MySheepActivity)
    }

    private val mDialog by lazy {
        DeleteDialog(this@MySheepActivity).also {
            it.onConfirm = {
                mAdapter.remove(deletePosition) {
                    goneLayout(mutableListOf(binding.Recycler), text = getString(R.string.no_data))
                }
                mResponse.delete(deleteID)
            }
        }
    }

    override fun layoutID() = R.layout.activity_my_sheep

    override fun initView() {
        StatusBarUtils.setStateAndNavigationBar(this@MySheepActivity, statusBarColor = com.example.lib.R.color.white)
        setTitle(getString(R.string.vaccines_msg25))
        binding.Recycler.verticalManager()
        binding.Recycler.adapter = mAdapter
        id = getIntentInt(JumpConstants.ID)
    }

    override fun onResume() {
        super.onResume()
        mResponse.getAllData {
            if (it.isEmpty()) {
                goneLayout(mutableListOf(binding.Recycler), text = getString(R.string.no_data))
            } else {
                mAdapter.updateList(it)
            }
        }
    }

    override fun initListener() {
        binding.Top.setOnDoubleClickListener {
            DisplayUtils.toMine(this@MySheepActivity)
        }

        binding.Go.setOnDoubleClickListener {
            DisplayUtils.toMine(this@MySheepActivity)
        }

        mAdapter.setOnClickListener(object : BaseRecyclerAdapter.OnClickListener<AnimalBean> {
            override fun click(bean: AnimalBean, position: Int) {
                if (id == 1 || id == 2) {
                    DisplayUtils.toAddAnimal(this@MySheepActivity, id, bean.toAnimalData())
                } else {
                    DisplayUtils.toUpdateSheep(this@MySheepActivity, bean.id.toInt())
                }
            }

            override fun other(bean: AnimalBean, position: Int) {
                deletePosition = position
                deleteID = bean.id
                mDialog.show()
            }
        })
    }
}